package fr.insta.reverse.uml.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import fr.insta.reverse.uml.bean.Guide;
import fr.insta.reverse.uml.bean.Projet;
import fr.insta.reverse.uml.util.HibernateUtil;

/**
 * DAO for domain model class Guide.
 * @see fr.insta.reverse.uml.bean.Guide
 * @author Valera
 */
@Stateless
public class DaoGuide implements IDao<Guide>
{

	private static final Log log = LogFactory.getLog(DaoGuide.class);

	@Override
	public Guide save(Guide gui)
	{
		log.debug("saving Guide instance");
		try
		{
			Session session = HibernateUtil.currentSession();
			Transaction tx = session.beginTransaction();

			session.saveOrUpdate(gui);

			tx.commit();
			HibernateUtil.closeSession();
			log.debug("save successful");
			return gui;
		}
		catch (RuntimeException e)
		{
			log.error("save failed", e);
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean remove(Guide gui)
	{
		log.debug("removing Guide instance");
		try
		{
			Session session = HibernateUtil.currentSession();
			Transaction tx = session.beginTransaction();

			gui = (Guide) session.createCriteria(Guide.class)
					.add(Restrictions.eq("lib", gui.getLib()))
					.add(Restrictions.eq("proj", gui.getProj()))
					.uniqueResult();

			session.delete(gui);

			tx.commit();
			HibernateUtil.closeSession();
			log.debug("remove successful");
			return true;
		}
		catch (RuntimeException e)
		{
			log.error("remove failed", e);
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Guide findById(int id)
	{
		log.debug("getting Guide instance with id: " + id);
		try
		{
			Session session = HibernateUtil.currentSession();

			Guide instance = (Guide) session.createCriteria(Guide.class)
					.add(Restrictions.eq("id", id))
					.uniqueResult();

			HibernateUtil.closeSession();
			log.debug("get successful");
			return instance;
		}
		catch (RuntimeException re)
		{
			log.error("get failed", re);
			re.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Guide> getAll()
	{
		log.debug("getting all Guides");
		List<Guide> listprojets = new ArrayList<Guide>();
		try
		{
			Session session = HibernateUtil.currentSession();

			listprojets = (List<Guide>) session.createCriteria(Guide.class).list();

			HibernateUtil.closeSession();
			log.debug("get successful");
			return listprojets;
		}
		catch (RuntimeException re)
		{
			log.error("get failed", re);
			re.printStackTrace();
		}
		return null;
	}

	public List<Guide> getAllByProject(Projet prj)
	{
		log.debug("getting all Guides by project");
		List<Guide> listprojets = new ArrayList<Guide>();
		try
		{
			Session session = HibernateUtil.currentSession();

			listprojets = (List<Guide>) session.createCriteria(Guide.class)
					.add(Restrictions.eq("proj", prj.getId()))
					.list();

			HibernateUtil.closeSession();
			log.debug("get successful");
			return listprojets;
		}
		catch (RuntimeException re)
		{
			log.error("get failed", re);
			re.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean deleteAll()
	{
		log.debug("deleting all Guides");
		try
		{
			Session session = HibernateUtil.currentSession();
			Transaction tx = session.beginTransaction();

			session.createQuery("delete from Guide").executeUpdate();

			tx.commit();
			HibernateUtil.closeSession();
			log.debug("delete successful");
			return true;
		}
		catch (RuntimeException re)
		{
			log.error("delete failed", re);
			re.printStackTrace();
		}
		return false;
	}
}
